﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using UtilsORM.MySqlData;
using ServiceWeb.DBGateway;
using UtilsBase;
using UtilsORM;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class SystemLogController : BaseMagerController
    {
        // GET:  Mager_SystemLog 1
        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, string Title = "", int PageIndex = 1, int PageSize = 15)
        {
            int totalRecord = 0;
            string sqlwhere = " and l_c_guid='" + LoginUserInfo.userCompanyGuid + "' ";
            ViewBag.Search_StartDate = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd");
            ViewBag.Search_EndDate = DateTime.Now.ToString("yyyy-MM-dd");
            List<SugarParameter> sugarParameters = new List<SugarParameter>();


            if (FC["StartDate"].Count > 0 && !string.IsNullOrEmpty(FC["StartDate"]))
            {
                sqlwhere = sqlwhere + " and l_addtime>=@StartDate";
                ViewBag.Search_StartDate = FC["StartDate"].ToString();

                sugarParameters.Add(new SugarParameter("StartDate", FC["StartDate"].ToString().Trim()));
            }
            else
            {
                sqlwhere = sqlwhere + " and l_addtime>='" + ViewBag.Search_StartDate + "' ";
            }

            if (FC["EndDate"].Count > 0 && !string.IsNullOrEmpty(FC["EndDate"]))
            {
                sqlwhere = sqlwhere + " and l_addtime<@EndDate ";
                ViewBag.Search_EndDate = FC["EndDate"].ToString();
                sugarParameters.Add(new SugarParameter("EndDate", Convert.ToDateTime(FC["EndDate"].ToString()).AddDays(1)));
            }
            else
            {
                sqlwhere = sqlwhere + " and l_addtime<'" + Convert.ToDateTime(ViewBag.Search_EndDate).AddDays(1).ToString("yyyy-MM-dd") + "' ";
            }


            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());
            }

            string sql = "select  * from bs_manage_system_log where 1=1  " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "l_id desc", ref totalRecord);


            ViewData["DataList"] = dt;

            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码
            return View(httpRequest);
        }
        public IActionResult list2(IFormCollection FC, string Title = "", int PageIndex = 1, int PageSize = 15)
        {
            int totalRecord = 0;
            string sqlwhere = " and l_c_guid='" + LoginUserInfo.userCompanyGuid + "' ";
            ViewBag.Search_StartDate = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd");
            ViewBag.Search_EndDate = DateTime.Now.ToString("yyyy-MM-dd");
            List<SugarParameter> sugarParameters = new List<SugarParameter>();


            if (FC["StartDate"].Count > 0 && !string.IsNullOrEmpty(FC["StartDate"]))
            {
                sqlwhere = sqlwhere + " and l_addtime>=@StartDate";
                ViewBag.Search_StartDate = FC["StartDate"].ToString();

                sugarParameters.Add(new SugarParameter("StartDate", FC["StartDate"].ToString().Trim()));
            }
            else
            {
                sqlwhere = sqlwhere + " and l_addtime>='" + ViewBag.Search_StartDate + "' ";
            }

            if (FC["EndDate"].Count > 0 && !string.IsNullOrEmpty(FC["EndDate"]))
            {
                sqlwhere = sqlwhere + " and l_addtime<@EndDate ";
                ViewBag.Search_EndDate = FC["EndDate"].ToString();
                sugarParameters.Add(new SugarParameter("EndDate", Convert.ToDateTime(FC["EndDate"].ToString()).AddDays(1)));
            }
            else
            {
                sqlwhere = sqlwhere + " and l_addtime<'" + Convert.ToDateTime(ViewBag.Search_EndDate).AddDays(1) + "' ";
            }


            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());
            }


            return View();
        }

    }
}